package me.heyclock.hl.recordwriter.robot;

import android.content.Context;
import android.content.DialogInterface;

import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;

import me.heyclock.hl.recordwriter.app.HomeActivity;
import me.heyclock.hl.recordwriter.tools.MediaPlayTool;

/**
 * 语音工具
 */
public class SpeechTool {
    /**
     * 获取听写对话框
     * @param context
     * @param recognizerDialogListener
     * @param onDismissListener
     * @return
     */
    public static RecognizerDialog getRecognizerDialog(Context context,
                                                       RecognizerDialogListener recognizerDialogListener,
                                                       DialogInterface.OnDismissListener onDismissListener){
        RecognizerDialog recognizerDialog = new RecognizerDialog(context, null);
        //清空参数
        recognizerDialog.setParameter(SpeechConstant.PARAMS, null);
        //设置引擎
        recognizerDialog.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
        //设置返回数据类型
        recognizerDialog.setParameter(SpeechConstant.RESULT_TYPE, "json");
        //设置中文-普通话
        recognizerDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
        recognizerDialog.setParameter(SpeechConstant.ACCENT, "mandarin");
        recognizerDialog.setListener(recognizerDialogListener);
        // 设置音频保存路径，保存音频格式支持pcm、wav，设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限
        // 注：AUDIO_FORMAT参数语记需要更新版本才能生效
        recognizerDialog.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
        //                    recognizerDialog.setParameter(SpeechConstant.ASR_AUDIO_PATH,
        //                            SystemUtils.getCacheDirectory(MyApplication.getInstance(), Environment.DIRECTORY_MUSIC) +
        //                                    "/msc/iat.wav");
        //                    recognizerDialog.setParameter(SpeechConstant.ASR_AUDIO_PATH,
        //                            AppUtils.RW_AUDIO_TEMP_PATH + "/sdcard/iat.wav");
        recognizerDialog.setOnDismissListener(onDismissListener);

        return recognizerDialog;
    }

    /**
     * 获取语音合成对象
     * @param context
     * @return
     */
    public static SpeechSynthesizer getSpeechSynthesizer(Context context){
        SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(context, null);
        //2.合成参数设置，详见《科大讯飞MSC API手册(Android)》SpeechSynthesizer 类
        mTts.setParameter(SpeechConstant.VOICE_NAME, "vinn");//设置发音人
        mTts.setParameter(SpeechConstant.SPEED, "50");//设置语速
        mTts.setParameter(SpeechConstant.VOLUME, "80");//设置音量，范围0~100
        mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); //设置云端
        //设置合成音频保存位置（可自定义保存位置），保存在“./sdcard/iflytek.pcm”
        //保存在SD卡需要在AndroidManifest.xml添加写SD卡权限
        //如果不需要保存合成音频，注释该行代码
        //mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, "./sdcard/iflytek.pcm");
        //3.开始合成
        //mTts.startSpeaking("嗯。我在，你说。", mSynListener);
        return mTts;
    }
}
